﻿Public Class SelectionDescripteur
    Inherits OpenDelibPage

    Private pThesaurus As String
    Public ReadOnly Property DescripteurSelectionne() As String
        Get
            If tvDescripteurs.CheckedNodes.Count = 0 Then Return ""
            Return tvDescripteurs.CheckedNodes(0).Text
        End Get
    End Property
    'Public ReadOnly Property ScriptDescripteurs As String
    '    Get
    '        Return CStr(ViewState("script_descripteurs"))
    '    End Get
    'End Property

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim appPath As String = Request.ApplicationPath
        If appPath = "/" Then appPath = ""
        AddJS(Page, "easyui", appPath & "/Scripts/jquery.easyui.min.js")
        AddCss(Page, appPath & "/Css/Autres/easyui.css")
        AddCss(Page, appPath & "/Css/Autres/icon.css")

        Odyssee.WebControles.JQPopupPanel.SetCancelScript(Me.btnAnnuler, False)

        pThesaurus = Request.QueryString("thesaurus")
        If pThesaurus = "" Then
            Me.lblErreur.Text = "Thésaurus obligatoire !"
            Return
        End If

        If Not IsPostBack Then
            Dim script As String = ""

            ' Chargement des descripteurs
            Using svc As New ServiceDescripteurs.DescripteursClient
                Dim params As New ServiceDescripteurs.ParametresRechercheDescripteurs
                params.Authentication = New ServiceDescripteurs.AuthenticationParameters
                params.Authentication.Token = Tools.GetInstance.getToken
                params.Thesaurus = pThesaurus

                Dim results As ServiceDescripteurs.ResultatTreeviewDescripteurs = Nothing
                Try
                    results = svc.RechercheTreeviewDescripteurs(params)
                    If results Is Nothing OrElse Not results.OK Then
                        Me.lblErreur.Text = "Impossible de charger les descripteurs du thésaurus '" & pThesaurus & "'" & If(results Is Nothing, ".", " :" & ControlChars.NewLine & results.ErrorMessage)
                        Return
                    Else
                        AjouterNode(Me.tvDescripteurs.Nodes, results.Descripteurs)
                        'script = "$('#cmbDescripteur').combotree({editable:true}); " & _
                        '         "$('#cmbDescripteur').combotree('loadData', ["
                        'AjouterNode(script, results.Descripteurs)
                        'script &= "]);"
                    End If
                Catch ex As Exception
                    NotifyErreur(ex.Message)
                End Try
            End Using

            ViewState("script_descripteurs") = script
        End If

        'If ScriptDescripteurs <> "" Then Me.ClientScript.RegisterStartupScript(Me.GetType, "ChargementCombo", ScriptDescripteurs, True)
    End Sub

    Private Sub AjouterNode(tnodes As TreeNodeCollection, nodes As List(Of ServiceDescripteurs.AcroTreeNode))
        For Each n As ServiceDescripteurs.AcroTreeNode In nodes
            Dim tn As New TreeNode(n.Libelle, n.CleDescripteur)
            tn.SelectAction = TreeNodeSelectAction.None
            tnodes.Add(tn)
            If n.Nodes.Count > 0 Then
                AjouterNode(tn.ChildNodes, n.Nodes)
            End If
        Next
    End Sub
    'Private Sub AjouterNode(ByRef script As String, nodes() As ServiceDescripteurs.AcroTreeNode)
    '    For i As Integer = 0 To nodes.Count - 1
    '        If i > 0 Then script &= ","
    '        script &= "{"
    '        script &= "id: '" & HttpUtility.JavaScriptStringEncode(nodes(i).CleDescripteur) & "'"
    '        script &= ",text: '" & HttpUtility.JavaScriptStringEncode(nodes(i).Libelle) & "'"
    '        If nodes(i).Nodes.Count > 0 Then
    '            script &= ",children:["
    '            AjouterNode(script, nodes(i).Nodes)
    '            script &= "]"
    '        End If
    '        script &= "}"
    '    Next
    'End Sub

    Private Sub btnOK_Click(sender As Object, e As EventArgs) Handles btnOK.Click
        'If Me.hfDescripteur.Value = "" Then
        '    Me.lblErreur.Text = "Vous devez sélectionner un descripteur !"
        '    Return
        'End If
        'Me.ClientScript.RegisterStartupScript(Me.GetType, "FermeturePopup", Odyssee.WebControles.JQPopupPanel.CreateOKScript(Me.hfDescripteur.Value), True)

        If Me.DescripteurSelectionne = "" Then
            Me.lblErreur.Text = "Vous devez sélectionner un descripteur !"
            Return
        End If

        Me.ClientScript.RegisterStartupScript(Me.GetType, "FermeturePopup", Odyssee.WebControles.JQPopupPanel.CreateOKScript(Me.DescripteurSelectionne), True)
    End Sub
End Class